package com.seele.tigerwallet.ui.activtiy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.MarketModel;
import com.seele.tigerwallet.ui.adapter.SortAdapter;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.event.SortEvent;
import com.seele.tigerwallet.ui.utils.DividerItemDecoration;
import com.seele.tigerwallet.ui.utils.GsonUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class SortActivity extends TitleBaseActivity {
    List<String> list = new ArrayList<>();

    private RecyclerView mRecyclerView;
    private SortAdapter mSortAdapter;

    private List<MarketModel> mBundleList;

    @Override
    public int getContainerViewId() {
        return R.layout.activity_sort;
    }

    @Override
    protected void initView() {

        setTitle(R.string.again_sort);
        mRecyclerView = $f(R.id.sort_recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mSortAdapter = new SortAdapter(R.layout.item_srot);
        mRecyclerView.setAdapter(mSortAdapter);
                mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
                LinearLayoutManager.HORIZONTAL, (int) getResources().getDimension(R.dimen.dp_1),
                getResources().getColor(R.color.color_DEDEDE))); //设置item的分割线

        mSortAdapter.setOnSortClickLitener(new SortAdapter.OnSortClickLitener() {
            @Override
            public void onTop(MarketModel item) {
                if (null != mBundleList && mBundleList.size() > 0) {
                    mBundleList.remove(item);
                    mBundleList.add(0, item);
                    mSortAdapter.setNewData(mBundleList);
                    mSortAdapter.notifyDataSetChanged();
                }


            }

            @Override
            public void onUp(MarketModel item, int position) {
                if (position == 0) {
                    return;
                }
                if (null != mBundleList && mBundleList.size() > 0) {
                    mBundleList.remove(item);
                    MarketModel marketModel = mBundleList.set(position - 1, item);
                    mBundleList.add(position, marketModel);
                    mSortAdapter.setNewData(mBundleList);
                    mSortAdapter.notifyDataSetChanged();
                }


            }

            @Override
            public void onLower(MarketModel item, int position) {
                if (null != mBundleList && mBundleList.size() > 0) {
                    if (position == mBundleList.size() - 1) {
                        return;
                    }
                    mBundleList.remove(item);
                    MarketModel marketModel = mBundleList.set(position, item);
                    mBundleList.add(position, marketModel);
                    mSortAdapter.setNewData(mBundleList);
                    mSortAdapter.notifyDataSetChanged();
                }

            }
        });

        initData();

    }

    @Override
    protected void onPause() {
        if (null != mBundleList && mBundleList.size() > 0) {
            for (int i = 1; i <= mBundleList.size(); i++) {
                mBundleList.get(i-1).position = i;
            }
            SortEvent sortEvent = new SortEvent(mBundleList);
            EventBus.getDefault().post(sortEvent);
        }

        super.onPause();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            mBundleList = (List<MarketModel>) bundle.getSerializable(SortActivity.class.getSimpleName());

        }
        mSortAdapter.setNewData(mBundleList);
        mSortAdapter.notifyDataSetChanged();

    }
}
